package com.kangyi.admin.controller;

import com.kangyi.admin.service.UserService;
import com.kangyi.dto.BulkOperationDTO;
import com.kangyi.dto.UserPageQueryDTO;
import com.kangyi.result.Result;
import com.kangyi.result.UserPageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/userAccount")
@Api(tags = "超级管理员-用户端账号管理相关接口")
@Slf4j
public class UserAccountController {

    @Autowired
    private UserService userService;

    @GetMapping("/page")
    @ApiOperation("用户端账号分页查询")
    public Result<UserPageResult> pageQuery(UserPageQueryDTO userPageQueryDTO){
        log.info("用户端账号分页查询：{}",userPageQueryDTO);
        try {
            UserPageResult userPageResult = userService.pageQuery(userPageQueryDTO);
            return Result.success(userPageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/operation")
    @ApiOperation("批量操作启用/禁用")
    @CacheEvict(cacheNames = "userAccount_", allEntries = true)
    public Result bulkOperation(@RequestBody BulkOperationDTO bulkOperationDTO){
        log.info("批量操作启用/禁用:{}",bulkOperationDTO);
        try {
            userService.bulkOperation(bulkOperationDTO);
            return Result.success("操作成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除账号")
    @CacheEvict(cacheNames = "userAccount_", allEntries = true)
    public Result deleteUserAccount(@RequestParam List<Integer> ids){
        log.info("批量删除账号:{}",ids);
        try {
            userService.deleteUserAccount(ids);
            return Result.success("删除成功!");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
